package com.situ.student.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.situ.student.controller.StudentController;

public class AddStudentFrame extends AddShareFrame {
	private StudentController studentController = new StudentController();
	private MainFrame mainFrame;

	public AddStudentFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	public void init() {
		super.init();
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("AddStudentFrame.init()");
				String name = nameText.getText().trim();
				System.out.println(name);
				int age = Integer.parseInt(ageText.getText().trim());
				System.out.println(age);
				String gender = genderText.getText().trim();
				System.out.println(gender);

				boolean result = studentController.insert(name, age, gender);
				if (result) {// 添加成功，弹出保存成功窗口，刷新表格
					// 保存成功或者失败，弹出提示框
					JOptionPane.showMessageDialog(null, "保存成功");
					mainFrame.reloadTable();
				} else {
					JOptionPane.showMessageDialog(null, "保存失败");
				}
				// 关闭窗口
				AddStudentFrame.this.dispose();
			}
		});
	}
}
